# import logging
#
#
# class MongoHandler(logging.Handler):
#  57     """
#  58     一个mongodb的log handler,支持日志按loggername创建不同的集合写入mongodb中
#  59     """
#
#       # msg_pattern = re.compile('(\d+-\d+-\d+ \d+:\d+:\d+) - (\S*?) - (\S*?) - (\d+) - (\S*?) - ([\s\S]*)')
#
#       def __init__(self, mongo_url, mongo_database='logs'):
#           """
#           :param mongo_url:  mongo连接
#           :param mongo_database: 保存日志的数据库，默认使用logs数据库
#           """
#           logging.Handler.__init__(self)
#           mongo_client = pymongo.MongoClient(mongo_url)
#           self.mongo_db = mongo_client.get_database(mongo_database)
#
#       def emit(self, record):
#           # noinspection PyBroadException,PyPep8
#           try:
#               """以下使用解析日志模板的方式提取出字段"""
#               # msg = self.format(record)
#               # logging.LogRecord
#               # msg_match = self.msg_pattern.search(msg)
#               # log_info_dict = {'time': msg_match.group(1),
#               #                  'name': msg_match.group(2),
#               #                  'file_name': msg_match.group(3),
#               #                  'line_no': msg_match.group(4),
#               #                  'log_level': msg_match.group(5),
#               #                  'detail_msg': msg_match.group(6),
#               #                  }
#               level_str = None
#               if record.levelno == 10:
#                   level_str = 'DEBUG'
#               elif record.levelno == 20:
#                   level_str = 'INFO'
#               elif record.levelno == 30:
#                   level_str = 'WARNING'
#               elif record.levelno == 40:
#                   level_str = 'ERROR'
#               elif record.levelno == 50:
#                   level_str = 'CRITICAL'
#               log_info_dict = OrderedDict()
#              log_info_dict['time'] = time.strftime('%Y-%m-%d %H:%M:%S')
#              log_info_dict['name'] = record.name             log_info_dict['file_path'] = record.pathname
#              log_info_dict['file_name'] = record.filename
#              log_info_dict['func_name'] = record.funcName
#              log_info_dict['line_no'] = record.lineno
#              log_info_dict['log_level'] = level_str
#              log_info_dict['detail_msg'] = record.msg
#              col = self.mongo_db.get_collection(record.name)
#              col.insert_one(log_info_dict)
#          except (KeyboardInterrupt, SystemExit):
#              raise
#          except Exception:
#              self.handleError(record)